package com.se2.antiqueexhibition.Service.impl;

import com.se2.antiqueexhibition.Mapper.AdminMapper;
import com.se2.antiqueexhibition.Service.AdminService;
import com.se2.antiqueexhibition.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(String username, String password) {
        // 密码加密
        String encryptedPassword = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        // 查询管理员
        Admin admin = adminMapper.selectByUsername(username);
        // 验证密码
        if (admin != null && admin.getPassword().equals(encryptedPassword)) {
            return admin;
        }
        return null;
    }

    @Override
    public boolean addAdmin(Admin admin) {
        // 检查用户名是否已存在
        if (isUsernameExists(admin.getUsername())) {
            return false;
        }
        // 检查手机号是否已存在
        if (admin.getPhone() != null && isPhoneExists(admin.getPhone())) {
            return false;
        }
        // 密码加密
        String encryptedPassword = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes(StandardCharsets.UTF_8));
        admin.setPassword(encryptedPassword);
        // 设置默认状态为启用
        admin.setStatus(1);
        // 插入管理员
        return adminMapper.insert(admin) > 0;
    }

    @Override
    public Admin getAdminById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public Admin getAdminByUsername(String username) {
        return adminMapper.selectByUsername(username);
    }

    @Override
    public List<Admin> getAllAdmins() {
        return adminMapper.selectAll();
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        return adminMapper.update(admin) > 0;
    }

    @Override
    public boolean updatePassword(Integer id, String oldPassword, String newPassword) {
        Admin admin = adminMapper.selectById(id);
        if (admin == null) {
            return false;
        }
        // 验证旧密码
        String encryptedOldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes(StandardCharsets.UTF_8));
        if (!admin.getPassword().equals(encryptedOldPassword)) {
            return false;
        }
        // 加密新密码
        String encryptedNewPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes(StandardCharsets.UTF_8));
        return adminMapper.updatePassword(id, encryptedNewPassword) > 0;
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        return adminMapper.updateStatus(id, status) > 0;
    }

    @Override
    public boolean deleteAdmin(Integer id) {
        return adminMapper.deleteById(id) > 0;
    }

    @Override
    public boolean isUsernameExists(String username) {
        return adminMapper.countByUsername(username) > 0;
    }

    @Override
    public boolean isPhoneExists(String phone) {
        return adminMapper.countByPhone(phone) > 0;
    }
}
